import Util from '../js/util'
import Const from '../js/const'

function timeFormat (timestamp, format) {
  if (!timestamp) {
    return '-'
  }

  if (!format) {
    format = 'Y-m-d H:i:s'
  }
  return Util.date(format, timestamp)
}

function digitLength (data, length) {
  return Util.sprintf('%0' + length + 'd', data)
}

function currencyFormat (money, type) {
  money = parseInt(money)
  if (money % 100 === 0 && !type) {
    return Util.sprintf('%.2f', money / 100)
  }
  return Util.sprintf('%.2f', parseInt(money) / 100)
}

function strToJson (str) {
  return JSON.parse(str)
}

function moneyFormat (money) {
  return money % 100 > 0 ? (money / 100.0).toFixed(2) : money / 100
}

function adType (type) {
  return Const.AD_TYPE[type]
}


const _filters = {
  timeFormat: timeFormat,
  digitLength: digitLength,
  currencyFormat: currencyFormat,
  strToJson: strToJson,
  moneyFormat: moneyFormat,
  adType: adType
}

export default (Vue) => {
  Object.keys(_filters).forEach(key => {
    Vue.filter(key, _filters[key])
  })
}
